<?php

namespace app\home\controller;

use think\Cookie;

//公共控制器
use app\common\controller\Homecontroller;

/**
 * 收藏控制器
 */
class Collect extends Homecontroller
{
    // 构造函数
    public function __construct()
    {
        //继承底层的控制器构造函数
        parent::__construct();

        //定义一个全局模型
        $this->PostModel = model('Post.Post');
        $this->UserModel = model('User.User');
        $this->CollectModel = model('User.Collect');
        $this->CommentModel = model('Post.Comment');
    }

    // 添加收藏
    public function add(){
        if($this->request->isPost()){

            // 信息提示
            $success = [
                'result' => false,
                'msg' => ''
            ];

            $id = $this->request->param('postid');

            // 帖子详情
            $postinfo = $this->PostModel->with(['cate','user'])->find($id);

            if(!$postinfo)
            {
                $this->error('帖子不存在');
                exit;
            }

            $userid = $this->LoginUser['id'];

            $userInfo = $this->UserModel->find($userid);

            if(!$userInfo){

                $success['msg'] = '没有这个人';
                $this->error('没有这个人');
                exit;
            }

            // 组装数据
            $data= [
                'userid' => $userid,
                'postid' => $id
            ];

            //判断当前这个人是否有收藏过
            $collect  = $this->CollectModel->where($data)->find();

            if($collect){
                $success['result'] = false;
                $success['msg'] = '您已收藏此贴，无须重复收藏';
                return json($success);
            }

            // 收藏表插入数据
            $result = $this->CollectModel->save($data);

            if($result === FALSE)
            {
                //添加收藏失败
                $success['result'] = false;
                $success['msg'] = $this->CollectModel->getError();
            }else
            {
                //添加收藏成功
                $success['result'] = true;
                $success['msg'] = '收藏成功';
            }

            //将结果返回给ajax
            return json($success);
        }
    }

    // 取消收藏
    public function del(){
        if($this->request->isPost()){

            // 信息提示
            $success = [
                'result' => false,
                'msg' => ''
            ];

            $id = $this->request->param('postid');

            // 帖子详情
            $postinfo = $this->PostModel->with(['cate','user'])->find($id);

            if(!$postinfo)
            {
                $this->error('帖子不存在');
                exit;
            }

            $userid = $this->LoginUser['id'];

            $userInfo = $this->UserModel->find($userid);

            if(!$userInfo){

                $success['msg'] = '没有这个人';
                $this->error('没有这个人');
                exit;
            }

            // 组装数据
            $data= [
                'userid' => $userid,
                'postid' => $id
            ];

            //判断当前这个人是否有收藏过
            $collect  = $this->CollectModel->where($data)->find();

            if(!$collect){
                $success['result'] = false;
                $success['msg'] = '您没有藏此贴，请去收藏';
                return json($success);
            }
            
            // 删除收藏表的数据
            $result = $this->CollectModel->where(['id' => $collect['id']])->delete();

            if($result === FALSE)
            {
                //取消收藏失败
                $success['result'] = false;
                $success['msg'] = $this->CollectModel->getError();
            }else
            {
                //取消收藏成功
                $success['result'] = true;
                $success['msg'] = '取消收藏成功';
            }

            //将结果返回给ajax
            return json($success);
        }
    }
}